package com.noble.glamour.ui.agora.act.voice;

import android.content.Context;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.noble.glamour.R;
import com.noble.glamour.module.logic.application.ModuleMgr;
import com.noble.glamour.module.util.UIShow;
import com.noble.glamour.ui.agora.RtcUIShow;
import com.noble.glamour.ui.agora.act.video.RtcChatInputPanel;
import com.noble.glamour.ui.agora.util.RtcJointChannel;
import com.noble.glamour.ui.utils.NoDoubleClickListener;
import com.noble.library.view.base.BasePanel;
import com.noble.library.view.CustomFrameLayout;

/**
 * 音频聊天底部浮层布局
 * <p>
 * Created by Su on 2017/7/18.
 */
public class RtcVoiceFootPanel extends BasePanel {
    private LinearLayout inputContainer;
    private CustomFrameLayout inputLayout;
    private ImageView btnSwitchMicro;        // 切换话筒
    private RtcChatInputPanel inputPanel;
    private ImageView voice_chat;

    public RtcVoiceFootPanel(Context context) {
        super(context);
        setContentView(R.layout.f1_chat_voice_foot_panel);
        initView();
    }

    private void initView() {
        btnSwitchMicro = (ImageView) findViewById(R.id.voice_switch);
        btnSwitchMicro.setImageResource(RtcJointChannel.getInstance().getSwitchMicro() ?
                R.drawable.p1_btn_switch_mianti2 : R.drawable.p1_btn_switch_mianti1);

        voice_chat = (ImageView) findViewById(R.id.voice_chat);
        if (ModuleMgr.getRtcEnginMgr().isCanChat()) { // 能发送
            voice_chat.setVisibility(View.VISIBLE);
            voice_chat.setOnClickListener(listener);
        }

        // 聊天输入框
        inputLayout = (CustomFrameLayout) findViewById(R.id.input_layout);
        inputLayout.show(R.id.chat_bottom);
        inputContainer = (LinearLayout) findViewById(R.id.chat_input_panel);
        inputPanel = new RtcChatInputPanel(getContext());
        inputContainer.addView(inputPanel.getContentView());
        inputPanel.setInputPanelListener(new RtcChatInputPanel.InputPanelListener() {
            @Override
            public void onInputbtnClick(View v) {
                refreshLayout();
            }
        });

        btnSwitchMicro.setOnClickListener(listener);
        findViewById(R.id.chat_gift).setOnClickListener(listener);
    }

    /**
     * 更新弹框布局
     */
    public void refreshLayout() {
        inputLayout.show(R.id.chat_bottom);
        inputPanel.refresh(View.GONE);
    }

    /**
     * 聊天开关
     */
    public void refreshChatBtn(boolean isCanChat) {
        voice_chat.setVisibility(isCanChat? View.VISIBLE : View.GONE);
        voice_chat.setOnClickListener(isCanChat ? listener : null);
    }

    private NoDoubleClickListener listener = new NoDoubleClickListener() {
        @Override
        public void onNoDoubleClick(View v) {
            switch (v.getId()) {
                case R.id.voice_switch:
                    RtcJointChannel.getInstance().switchMicro();
                    break;

                case R.id.chat_gift:
                    RtcUIShow.showRtcBottomGiftDlg(getContext(), ModuleMgr.getRtcEnginMgr().getEngineConfig().mOtherId);
                    break;

                case R.id.voice_chat:
                    inputLayout.show(R.id.chat_input_panel);
                    inputPanel.refresh(View.VISIBLE);
                    break;
            }
        }
    };

    public void refreshSwitchMicro(boolean value) {
        btnSwitchMicro.setImageResource(value ? R.drawable.p1_btn_switch_mianti2 :
                R.drawable.p1_btn_switch_mianti1);
    }
}
